package com.lanxin.stu.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanxin.stu.dto.ClassesAddDTO;
import com.lanxin.stu.dto.ClassesCheckDTO;
import com.lanxin.stu.dto.ClassesDTO;
import com.lanxin.stu.dto.ClassesSearchDTO;
import com.lanxin.stu.entity.Classes;
import com.lanxin.stu.mapper.ClassesMapper;
import com.lanxin.stu.mapstruct.ClassesMapStruct;
import com.lanxin.stu.service.IClassesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanxin.stu.vo.ClassesVO;
import com.lanxin.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cyh
 * @since 2025-09-17
 */
@Service
public class ClassesServiceImpl extends ServiceImpl<ClassesMapper, Classes> implements IClassesService {

    @Autowired
    private ClassesMapStruct classesMapStruct;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public PageVO<ClassesVO> listPageClasses(ClassesSearchDTO classesSearchDTO) {

        Page<Classes> pageDTO = new Page<>(classesSearchDTO.getPage(),classesSearchDTO.getPageSize());
        QueryWrapper<Classes> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(classesSearchDTO.getCname())){
            queryWrapper.like("cname",classesSearchDTO.getCname());
        }
        IPage<Classes> iPage = baseMapper.selectPage(pageDTO, queryWrapper);
        IPage<ClassesVO> pageVO= iPage.convert(classes -> classesMapStruct.toVO(classes));
        return  new PageVO<ClassesVO>(pageVO);
    }

    @Override
    public boolean addClasses(ClassesAddDTO classesAddDTO) {
        Classes classes = new Classes();
        classes.setCname(classesAddDTO.getCname());
        int id = baseMapper.insert(classes);
        if(id>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public ClassesVO findClassesById(Integer id) {
        ClassesVO classesVO = (ClassesVO) redisTemplate.opsForValue().get("classes:"+id);
        if (classesVO==null){
            Classes classes = baseMapper.selectById(id);
            classesVO= classesMapStruct.toVO(classes);
            redisTemplate.opsForValue().set("classes:"+id,classesVO);
        }
        return classesVO;
    }

    @Override
    public boolean updateClasses(ClassesDTO classesDTO) {
        int row = baseMapper.updateById(classesMapStruct.toEntity(classesDTO));
        if (row>0){
            redisTemplate.delete("classes:"+classesDTO.getId());
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean deleteClasses(Integer id) {
        int row = baseMapper.deleteById(id);
        if (row>0)
        {
            redisTemplate.delete("classes:"+id);
            return true;
        }
        return false;
    }

    /**
     * 检查班级名称是否已存在
     *
     * @param classesCheckDTO 包含班级名称和ID的检查参数对象
     * @return true表示班级名称可用(不存在或为当前班级本身)，false表示班级名称已存在
     */
    @Override
    public boolean checkClassName(ClassesCheckDTO classesCheckDTO) {
        // 构造查询条件，只查询id字段以提高性能
        QueryWrapper<Classes> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id");
        if (classesCheckDTO.getCname() == null) {
            return true;
        }
        queryWrapper.eq("cname", classesCheckDTO.getCname());
        Classes classes = baseMapper.selectOne(queryWrapper);

        // 如果查询到同名班级且不是当前班级本身，则返回false表示名称已存在
        if (classes != null && classes.getId() != null && classesCheckDTO.getId() != null
                && !classes.getId().equals(classesCheckDTO.getId())) {
            return false;
        }
        return true;
    }


}
